//
//  YWWeiboController.m
//  videoTest3
//
//  Created by jun peng on 2022/6/17.
//

#import "YWWeiboController.h"
#import "YWGetNewsListParam.h"
#import "YWGetNewsListStatus.h"
#import "YWGetNewsResultStatus.h"
#import "YWGetNewsDataStatus.h"
#import "YWGetNewsTool3.h"
#import "YWListCellDelegate.h"
#import "YWNoContentView2.h"
#import "YWHomeInfoCell.h"
#import "YWHomeRecommendVedioCell.h"
#import "YWHomeRecommendLittleVedioCell.h"
#import "YWHomeRecommendLiveCell.h"
#import "YWClassifyTool.h"
#import "YWClassifyStatus.h"
#import "YWPhotoBrowserAnimator.h"
#import "YWPhototBrowserController.h"
#import "YWNavigationController.h"

@interface YWWeiboController ()<UITableViewDataSource, UITableViewDelegate,YWListCellDelegate,YWNoContentView2Delegate,YWPhotoBrowserAnimatorPresentedDelegate,YWPhotoBrowserAnimatorDismissDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *barConstraintHeight;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *statusArray;

@property (nonatomic,assign)NSInteger page;

//转场动画
@property(nonatomic,strong)YWPhotoBrowserAnimator *animator;

@end

@implementation YWWeiboController

#pragma mark - 懒加载
-(YWPhotoBrowserAnimator *)animator{
    if (_animator == nil) {
        _animator = [[YWPhotoBrowserAnimator alloc] init];
    }
    return _animator;
}


-(NSMutableArray *)statusArray{
    if (_statusArray == nil) {
        _statusArray = [NSMutableArray array];
    }
    return _statusArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.title = @"微博";
    self.barConstraintHeight.constant = self.navBar.frame.size.height;
    
    MJWeakSelf
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [weakSelf loadNewsData];

    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPhotoBrowser:) name:ShowPhotoBrowserNote object:nil];
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

-(void)showPhotoBrowser:(NSNotification *)notification{
    
    //因为通知有一对多、所以要增加一个判断防止和其它VC同时接收
    
    YWNavigationController *nav = self.tabBarController.selectedViewController;
    if (nav.topViewController != self) {
        return;
    }
    
    NSDictionary *userInfo = notification.userInfo;
    
    NSIndexPath *indexPath = userInfo[ShowPhotoBrowserIndexKey];
    
    NSArray *iconStatusArray = userInfo[ShowPhotoBrowserUrlsKey];
    
    id object = notification.object;
    
    YWPhototBrowserController *vc = [[YWPhototBrowserController alloc] initWithImgBeginIndexPath:indexPath picUrlArray:iconStatusArray];
    
    // 2.设置modal样式
    vc.modalPresentationStyle = UIModalPresentationCustom;
    
    // 3.设置转场的代理
    vc.transitioningDelegate = (id<UIViewControllerTransitioningDelegate>)self.animator;
  
    
    // 4.设置动画的代理
    self.animator.presentedDelegate = object;
    self.animator.indexPath = indexPath;
    self.animator.dismissDelegate = (id<YWPhotoBrowserAnimatorDismissDelegate>)vc;

    // 以modal的形式弹出控制器
    [self presentViewController:vc animated:YES completion:nil];
    
    // 1.创建控制器
//    let photoBrowserVc = PhotoBrowserController(indexPath: indexPath, picURLs: picURLs)
//
//    // 2.设置modal样式
//    photoBrowserVc.modalPresentationStyle = .Custom
    
    // 3.设置转场的代理
//    photoBrowserVc.transitioningDelegate = photoBrowserAnimator
    
    // 4.设置动画的代理
//    photoBrowserAnimator.presentedDelegate = object
//    photoBrowserAnimator.indexPath = indexPath
//    photoBrowserAnimator.dismissDelegate = photoBrowserVc
    
    // 以modal的形式弹出控制器
//    presentViewController(photoBrowserVc, animated: true, completion: nil)
    
}


//加载新数据
-(void)loadNewsData{
    
    self.page = 1;
       YWGetNewsListParam *param = [[YWGetNewsListParam alloc] init];
    NSString *title = @"投顾论市";
    param.label = title;
       param.pagesize = @"20";
       param.currentpage = [NSString stringWithFormat:@"%ld",(long)self.page];
//       param.contenttype = @"1";
       NSString *userID = self.meStatus.userStatus.Id;
          if (userID != nil) {
              param.userid = userID;
          }
          else{
              
              param.userid = @"";
          }
    
    [YWClassifyTool getClassifyListStatusesWithParameters:param success:^(YWClassifyStatus * _Nonnull status) {
        
        [self.noContentView removeFromSuperview];
        [self.tableView.mj_header endRefreshing];
               if (status.IsSuccess.integerValue == 1) {
                   
                   
                   self.statusArray = [NSMutableArray arrayWithArray:status.ResultObj.PageInfo.nList];
                   
                   for (NSInteger i = 0; i<self.statusArray.count; i++) {
                       YWGetNewsDataStatus *dataStatus = self.statusArray[i];
                    
                       dataStatus.isClassify = YES;
                   }
                   
                   [self.tableView reloadData];
                   
                   //假如少于就不需要添加上拉控件了
                   if (status.ResultObj.PageInfo.nList.count == param.pagesize.integerValue) {
                       MJWeakSelf
                       //添加下拉刷新控件
                       self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

                           [weakSelf loadMoreData];
                       }];
                   }
                   
               }
               else{
                   
                   self.statusArray = [NSMutableArray array];
                   
                   [self.tableView reloadData];
                   
                   [self.view addSubview:self.noContentView];
                   
                   self.noContentView.frame = CGRectMake(0, 0, Wi, self.view.height);
                   
                   self.noContentView.noContentType = YWNoContentView2Type_NoContent;
                   
               }
    } failure:^(NSError * _Nonnull error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.view addSubview:self.noContentView];
        
        self.noContentView.frame = CGRectMake(0, 0, Wi, self.view.height);
        
        self.noContentView.noContentType = YWNoContentView2Type_Error404;
        
    }];
    
       
}

-(void)noContentView2DidClickRefreshBtn:(YWNoContentView2 *)noContentView2{
    
    [self loadNewsData];
   
    
}

//加载更多
-(void)loadMoreData{
    
    self.page ++;
    YWGetNewsListParam *param = [[YWGetNewsListParam alloc] init];
    NSString *title = @"投顾论市";
    param.label = title;
    param.pagesize = @"20";
    param.currentpage = [NSString stringWithFormat:@"%ld",(long)self.page];
//    param.contenttype = @"1";
    NSString *userID = self.meStatus.userStatus.Id;
       if (userID != nil) {
           param.userid = userID;
       }
       else{
           
           param.userid = @"";
       }
    
    
    
    [YWClassifyTool getClassifyListStatusesWithParameters:param success:^(YWClassifyStatus * _Nonnull status) {
        
        //移除上拉
        [self.tableView.mj_footer endRefreshing];
        if (status.IsSuccess.integerValue == 1) {
            
            [self.statusArray addObjectsFromArray:status.ResultObj.PageInfo.nList];
            
            
            for (NSInteger i = 0; i<self.statusArray.count; i++) {
                YWGetNewsDataStatus *dataStatus = self.statusArray[i];
               
                dataStatus.isClassify = YES;
                
            }

            [self.tableView reloadData];
            
        }
        else{
            
            //它做的接口没数据是走这里的、所以可以直接移除footer
            [self.tableView.mj_footer removeFromSuperview];
            [MBProgressHUD showError:status.Message];
            
        }
    } failure:^(NSError * _Nonnull error) {
        
        //移除上拉
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD showError:@"服务器繁忙，请稍后再试"];
    }];
    

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YWGetNewsDataStatus *status = self.statusArray[indexPath.row];
    
    if (status.cellType == YWCellTypeNoImg) {
        
        YWHomeInfoCell *cell = [YWHomeInfoCell cellWithTableView:tableView];
        
        cell.status = status;
        cell.delegate = self;
        
         return cell;
    }
    else if (status.cellType == YWCellTypeOneImg){
        
        YWHomeInfoCell *cell = [YWHomeInfoCell cellWithTableView:tableView];
        
        cell.status = status;
        cell.delegate = self;
        
         return cell;
        
    }
    else if (status.cellType == YWCellTypeVideo){
        
        YWHomeRecommendVedioCell *cell = [YWHomeRecommendVedioCell cellWithTableView:tableView];
        
        cell.status = status;
        cell.delegate = self;
        
         return cell;
        
    }
    else{
        
        YWHomeRecommendLittleVedioCell *cell = [YWHomeRecommendLittleVedioCell cellWithTableView:tableView];
        cell.status = status;
        cell.delegate = self;
        
        return cell;
        
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    tableView.rowHeight = UITableViewAutomaticDimension;

    tableView.estimatedRowHeight = 416;

    return tableView.rowHeight;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.statusArray.count;
}

@end
